<template>
  <div class="app-container">

    <div class="filter-container">
      <!-- <el-select
        v-model="listQuery.error_type"
        style="width: 150px"
        class="filter-item"
        @change="handleFilter"
      >
         <el-option label="全部状态" :value="''" />
        <el-option v-for="(v, k) in error_type" :key="k" :label="v" :value="k" />
        
      </el-select> -->
      <el-select
          v-model="listQuery.query_type"
          style="width: 150px"
          class="filter-item"
          @change="handleFilter"
        >
          <el-option label="实时" :value="'1'" />
          <el-option label="历史" :value="''" />
        </el-select>

        <el-date-picker
          v-if="listQuery.query_type != '1'"
          v-model="listQuery.date"
          type="date"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          placeholder="选择日期">
        </el-date-picker>
      <el-input
       v-if="listQuery.query_type != '1'"
        v-model="listQuery.adv_space"
        placeholder="请输入广告位"
        style="width: 220px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <!-- <el-input
        v-model="listQuery.message"
        placeholder="请输入message"
        style="width: 220px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      /> -->
      <el-button
       plain size="small"
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>

      <el-button
       plain size="small"
        v-waves
        class="filter-item btn"
        type="primary"
        icon="el-icon-refresh"
        @click="handleCancel"
      >重置</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-table
              :data="props.row.template"
              border
              style="width: 100%">
              <el-table-column prop="template_id" label="模板id" align="center"> </el-table-column>
              <el-table-column prop="prepare_show" align="center" label="上报展示数"> </el-table-column>
              <el-table-column prop="show" align="center" label="回调展示数"> </el-table-column>

              <el-table-column label="展现成功率" min-width="" align="center">
                <template slot-scope="{row}">
                  <span>{{ (row.show / row.prepare_show * 100).toFixed(2) }}%</span> 
                </template>
              </el-table-column>

              <el-table-column prop="click" label="点击" align="center"> </el-table-column>

              <el-table-column label="点击率" min-width="" align="center">
                <template slot-scope="{row}">
                  <span>{{ (row.click / row.show * 100).toFixed(2) }}%</span> 
                </template>
              </el-table-column>
              
            </el-table>
          </el-form>

        </template>
      </el-table-column>
      <el-table-column label="时间" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.time_ }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告位" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ row.adv_space }}</span> 
        </template>
      </el-table-column>
      <el-table-column label="上报展示" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ row.prepare_show }}</span> 
        </template>
      </el-table-column>
      <el-table-column label="回调展示数" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ row.show }}</span> 
        </template>
      </el-table-column>
      <el-table-column label="展现成功率" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ (row.show / row.prepare_show *100).toFixed(2) }}%</span> 
        </template>
      </el-table-column>
      <el-table-column label="点击数量" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ row.click }}</span> 
        </template>
      </el-table-column>
      
      <el-table-column label="点击率" min-width="" align="center">
        <template slot-scope="{row}">
          <span>{{ (row.click / row.show *100).toFixed(2) }}%</span> 
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      class="pageShow"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  getTemplateReport
} from "@/api/adv_admin/config";
// import {
//   quickLogin,
// } from "@/api/admin/control";addProxyOrqudao
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
export default {
  name: "Control",
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        adv_space: "",
        query_type: '1',
        date: ''
      },
      error_type: {},
      pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
        },
    };
  },
  created() {
    this.getList();
  //   this.setDialogWidth();
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.setDialogWidth();
      })();
    };
  },
  methods: {
    setDialogWidth() {
      const val = document.body.clientWidth;
      const def = 600; // 默认宽度
      if (val < def) {
        this.dialogWidth = "100%";
      } else {
        this.dialogWidth = def + "px";
      }
    },
    getList() {
      this.listLoading = true;
      getTemplateReport(this.listQuery).then((response) => {
        this.list = response.DATA.list;
        this.total = response.DATA.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCancel(){
      this.listQuery = {
        page: 1,
        limit: 20,
        adv_space: "",
        query_type: '1',
        date: ''
      }
      this.getList()
    }
  
  },
};
</script>

<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.push {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin-left: 5px;
}
.pageHidden {
  display: none;
}
.btn {
  margin-left: 0;
}
.fullwidth {
  width: 450px;
  margin-left: 50px;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .fullwidth {
    width: 100%;
    margin-left: 0;
  }
}
</style>
